let con = require('../mysql/mysql');

let index = async (req, res) => {
  res.send('如你所见，已启动！');
};
// 登录
let Login = async (req, res) => {
  con.connect();
  let params = [req.body.user, req.body.psd];
  let result = await con.login(params);
  console.log(result);
  let flag = await result.length;
  flag
    ? res.send({ code: 200, msg: '登录成功!' })
    : res.send({ code: 201, msg: '账号或者密码错误' });
};
// 注册
let register = async (req, res) => {
  let params = await {
    user: req.body.user,
    psd: req.body.psd,
    phoneNum: req.body.phoneNum,
    age: req.body.age,
    sex: req.body.sex,
  };
  console.log(params);
  let select = await con.find('user', `user='${params.user}'`);
  console.log(select.length);
  if (select.length == 0) {
    res.send(await con.insert('user', params));
  } else {
    res.send({
      code: 201,
      msg: '账号已存在！',
    });
  }
};
// 公交路线
// let busList = async(req, res) => {
//         con.connect()
//         res.send(await con.find('bus'))
//     }
// 新闻
let hotNews = async (req, res) => {
  con.connect();
  res.send(await con.find('hotNews'));
};
module.exports = {
  index,
  // busList,
  hotNews,
  Login,
  register,
};
